package com.example.smartcity.ui.selfCenter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.SeedBackBean;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.android.material.snackbar.Snackbar;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Feedback extends AppCompatActivity {

    private EditText ETFeedBack;
    private TextView letter;
    private Button btnSend;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ETFeedBack = (EditText) findViewById(R.id.ET_feedBack);
        letter = (TextView) findViewById(R.id.letter);
        btnSend = (Button) findViewById(R.id.btn_send);
        ControlLetter();
        submit();
    }
    public void setBack(View view){
        this.finish();
    }
    //字数控制
    public void ControlLetter(){
        ETFeedBack.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                letter.setText(s.length()+"/150");
            }
        });
    }
    //意见反馈
    public void submit(){
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String Content = ETFeedBack.getText().toString();
                if (Content.length()>0){
                    SharedPreferences sharedPreferences = getSharedPreferences("selfMessage",MODE_PRIVATE);
                    String userId = sharedPreferences.getString("userID",null);
                    String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/feedback";
                    SeedBackBean bean = new SeedBackBean();
                    bean.setContent(Content);
                    bean.setUserId(userId);
                    Gson gson = new Gson();
                    String json = gson.toJson(bean);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),json);
                    OkHttpUtils.getCall(newUrl,requestBody,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.d("TAG", "onFailure: "+e.getMessage());
                            if (e.getMessage().equals("timeout")){
                                Snackbar.make(v,"发送超时",Snackbar.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String BackJson = response.body().string();
                            stateBean stateBean = gson.fromJson(BackJson, com.example.smartcity.ui.Bean.stateBean.class);
                            Snackbar.make(v,stateBean.getMsg(),Snackbar.LENGTH_SHORT).show();
                        }
                    });
                }else{
                    Snackbar.make(v,"并没有输入任何东东",Snackbar.LENGTH_SHORT).show();
                }


            }
        });
    }
}